package keeto.com.vn.classified.menutab;

import java.io.IOException;
import java.util.ArrayList;

import keeto.com.vn.classfied.model.Area;
import keeto.com.vn.classified.database.BuiltinServiceManager;
import keeto.com.vn.classified.manager.IAManager;
import keeto.com.vn.classified.ui.R;

import org.xmlpull.v1.XmlPullParserException;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources.NotFoundException;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MenuTabAdapter extends BaseAdapter{

	private Context context;
	private ArrayList<MenuTabModel> tabModel;
	private LayoutInflater inflater;
	private boolean isLogon;
	private String username;
	private int current_index = 0 ;
	private Area mArea;
	private BuiltinServiceManager manager;
	
	public MenuTabAdapter(Context context, boolean islogon, int current_Idx) {
		this.context = context;
		this.inflater = LayoutInflater.from(context);
		this.isLogon = islogon;
		this.current_index = current_Idx;
		this.mArea = IAManager.getInstance(context).getMyArea();
		this.manager = BuiltinServiceManager.getInstance(context);
		initDataMenuTabModel(context ,islogon);
	}


	public void setUsername(String username) {
		this.username = username;
	}


	public ArrayList<MenuTabModel> getTabModel() {
		if (tabModel == null){
			return null;
		}else{
			return tabModel;

		}
	}

	public boolean isLogon() {
		return isLogon;
	}
	public void setLogon(boolean isLogon) {
		this.isLogon = isLogon;
		initDataMenuTabModel(context, isLogon);
	}
	
	
	private void initDataMenuTabModel(Context ctx, boolean islogon){
		tabModel = new ArrayList<MenuTabModel>();
		if (islogon == true){
			tabModel.add(new MenuTabModel(ctx.getResources().getString(R.string.menu_bar_home),IMenuTabConfig.MENU_UNINITIALIZED));
			tabModel.add(new MenuTabModel(ctx.getResources().getString(R.string.menu_bar_category),IMenuTabConfig.MENU_HOME));
			tabModel.add(new MenuTabModel(ctx.getResources().getString(R.string.menu_bar_area),IMenuTabConfig.MENU_UNINITIALIZED));
			if (mArea != null){
				tabModel.add(new MenuTabModel(mArea.getName(),IMenuTabConfig.MENU_AREA));

			}else{
				tabModel.add(new MenuTabModel(ctx.getResources().getString(R.string.menu_bar_area_all),IMenuTabConfig.MENU_AREA));
			}
			tabModel.add(new MenuTabModel(ctx.getResources().getString(R.string.menu_bar_profile),IMenuTabConfig.MENU_UNINITIALIZED));
			tabModel.add(new MenuTabModel(ctx.getResources().getString(R.string.menu_bar_post),IMenuTabConfig.MENU_POST));
			tabModel.add(new MenuTabModel(manager.getBuitinService().getUserName(),IMenuTabConfig.MENU_PROFILE_NAME));
			tabModel.add(new MenuTabModel(ctx.getResources().getString(R.string.menu_bar_interst),IMenuTabConfig.MENU_INTEREST));
			tabModel.add(new MenuTabModel(ctx.getResources().getString(R.string.menu_bar_posted),IMenuTabConfig.MENU_POSTED));
			tabModel.add(new MenuTabModel(ctx.getResources().getString(R.string.menu_bar_history),IMenuTabConfig.MENU_HISTORY));
			tabModel.add(new MenuTabModel(ctx.getResources().getString(R.string.menu_bar_logout),IMenuTabConfig.MENU_LOGOUT));

		}else{
			tabModel.add(new MenuTabModel(ctx.getResources().getString(R.string.menu_bar_home),IMenuTabConfig.MENU_UNINITIALIZED));
			tabModel.add(new MenuTabModel(ctx.getResources().getString(R.string.menu_bar_category),IMenuTabConfig.MENU_HOME));
			tabModel.add(new MenuTabModel(ctx.getResources().getString(R.string.menu_bar_area),IMenuTabConfig.MENU_UNINITIALIZED));
			if (mArea != null){
				tabModel.add(new MenuTabModel(mArea.getName(),IMenuTabConfig.MENU_AREA));

			}else{
				tabModel.add(new MenuTabModel(ctx.getResources().getString(R.string.menu_bar_area_all),IMenuTabConfig.MENU_AREA));
			}
			tabModel.add(new MenuTabModel(ctx.getResources().getString(R.string.menu_bar_profile),IMenuTabConfig.MENU_UNINITIALIZED));
			tabModel.add(new MenuTabModel(ctx.getResources().getString(R.string.menu_bar_post),IMenuTabConfig.MENU_POST));
			tabModel.add(new MenuTabModel(ctx.getResources().getString(R.string.menu_bar_login),IMenuTabConfig.MENU_PROFILE_NAME));

		}

	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return tabModel.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return tabModel.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder;
		if (convertView == null){
			convertView = inflater.inflate(R.layout.item_list_menutab_layout,null);
			holder = new ViewHolder();
			holder.tvName = (TextView)convertView.findViewById(R.id.tv_name_menubar);
			holder.rlView = (RelativeLayout)convertView.findViewById(R.id.rl_item_menuTab);
			holder.img_arraw = (ImageView)convertView.findViewById(R.id.img_arraw_tab);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}
		holder.tvName.setText(tabModel.get(position).getNameMenu());
		if (tabModel.get(position).getMenutab_key() == IMenuTabConfig.MENU_UNINITIALIZED){
			holder.rlView.setBackgroundColor(context.getResources().getColor(R.color.search_buddy_bg));
			holder.tvName.setTypeface(null, Typeface.BOLD);
			holder.tvName.setTextColor(context.getResources().getColor(R.color.tv_menubar_color));
			holder.img_arraw.setVisibility(View.INVISIBLE);
		}else{
			holder.rlView.setBackgroundColor(context.getResources().getColor(R.color.white));
			try {
				ColorStateList listColor = ColorStateList.createFromXml(context.getResources(),context.getResources().getXml(R.color.menubar_textcolor));
				holder.tvName.setTextColor(listColor);
			} catch (NotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (XmlPullParserException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			holder.tvName.setTypeface(null, Typeface.NORMAL);
			holder.img_arraw.setVisibility(View.VISIBLE);
		}
		if (tabModel.get(position).getMenutab_key() == current_index){
			holder.tvName.setTypeface(null, Typeface.BOLD);
			holder.tvName.setTextColor(context.getResources().getColor(R.color.tv_menubar_color_selected));
		}
		return convertView;
	}

	class ViewHolder {
		private TextView tvName;
		private RelativeLayout rlView;
		private ImageView img_arraw;
	}
}
